<?php

namespace app\common\model;

use think\Db;
use think\Model;

class BasicUser extends Model
{

    // 指定表名,不含前缀
    protected $name = 'basic_user';
    protected $tableName = 'ex_basic_user';


    public function getListByArg(array $arg,$field='*',$group_by = null)
    {
        return $this->where($arg)->field($field)->group($group_by)->select();
    }

    public function getListByArgOne(array $arg,$field='*')
    {
        return $this->where($arg)->field($field)->find();
    }

    public function getChildAdminReg(){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $cplaform = '1';
        if (count($child_plaform)>1){
            foreach ($child_plaform as $kc=>$kv)
            {
                $cplaform .=",'".$kv."' ";
            }
            $cplaform .=",'".$kv."'";
        }else{
            $cplaform = implode(',',$child_plaform);
            $cplaform = "'".$cplaform."'";
        }
        return $cplaform;
    }

    /* 获取USER表中人所有用户到培训室 */

    public function getAllUserToBasic($basicid, $userid = 0)
    {
        if (empty($basicid))
            return false;
        try {
            $cplaform = $this->getChildAdminReg();
//            $sql = "replace into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) ";
            $sql = "select uc.id identity_id,u.userid gdl_userid,ep.id plaform_id,u.username,{$basicid} basic_id,u.realname usertruename,uc.company_id,uc.company_name company,u.idcard,uc.departmentid ";
            $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg "; //排除超级管理员
            $sql .= "where uc.admin_reg in ({$cplaform}) and uc.userid>0 and uc.status=0 and uc.isdelete=0";
            if (!empty($userid)) {
                if (is_array($userid))
                    $userid = implode(',', $userid);
                $sql .= " and uc.userid in ('{$userid}') ";
            }
            $sql .= " and uc.id not in (select identity_id from ".$this->tableName." where basic_id=" . $basicid;

            if (!empty($userid))
                $sql .= " and userid in('{$userid}')";
            $sql .= ") group by uc.id";

            $res = $this->query($sql);
            if (empty($res)){
                return false;
            }
            //获取当前平台的身份ID
            $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from {$this->tableName} where basic_id={$basicid})";
            $res1 = $this->query($sql1);
            foreach ($res as $key =>$valuse ){
                foreach ($res1 as $key1 => $val1 ){
                    if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                        unset($res[$key]);
                    }
                }
            }
            if (!empty($res)){
                $value_str = '';
                foreach($res as $v){
                    $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['basic_id'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."',".$v['departmentid']."),";
                }

                $valueStr = rtrim($value_str,',');
                $sql2 = "INSERT into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) values $valueStr";
//              var_dump($sql);die;
                $this->execute($sql2);
            }

        } catch (Exception $ex) {
            \SLogger::getLogger();
            return FALSE;
        }
        return TRUE;
    }

    public function importUserById($id, $userid)
    {
        if (empty($id))
            return false;
        if (empty($userid))
            return false;
        if (is_array($userid))
            $userid = implode(',', $userid);
        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ".$this->tableName."(identity_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) ";
        $sql = " select uc.id identity_id,u.userid gdl_userid,ep.id plaform_id,u.username,{$id} basic_id,u.realname usertruename,uc.company_id,uc.company_name company,u.idcard,uc.departmentid ";
        $sql .= " from ex_user_company as uc left join ex_user as u on uc.userid=u.userid  left join ex_plaform as ep on uc.admin_reg=ep.admin_reg";
        $sql .= " where uc.admin_reg in ({$cplaform}) and uc.userid>0 and uc.id in({$userid }) and uc.status=0 ";
        $sql .= " and uc.id not in (select ex_basic_user.identity_id from ex_basic_user where basic_id={$id} and identity_id in({$userid}))";
//        var_dump($sql);die;
        $res = $this->query($sql);
        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from {$this->tableName} where basic_id={$id})";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
                $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['basic_id'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."',".$v['departmentid']."),";
            }

            $valueStr = rtrim($value_str,',');
            $sql2 = "INSERT into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) values $valueStr";
//              var_dump($sql);die;
            $this->execute($sql2);
        }
        return true;
    }
	
	
	public function importUserByIds($id, $userids)
    {
		
		$userid =  $userids['identity_id'];
        if (empty($id))
            return false;
        if (empty($userid))
            return false;
        if (is_array($userid))
            $userid = implode(',', $userid);
        $cplaform = $this->getChildAdminReg();
//        $sql = "insert into ".$this->tableName."(identity_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) ";
        $sql = " select uc.id identity_id,u.userid gdl_userid,ep.id plaform_id,u.username,{$id} basic_id,u.realname usertruename,uc.company_id,uc.company_name company,u.idcard,uc.departmentid ";
        $sql .= " from ex_user_company as uc left join ex_user as u on uc.userid=u.userid  left join ex_plaform as ep on uc.admin_reg=ep.admin_reg";
        $sql .= " where uc.admin_reg in ({$cplaform}) and uc.userid>0 and uc.id in({$userid }) and uc.status=0 ";
        $sql .= " and uc.id not in (select ex_basic_user.identity_id from ex_basic_user where basic_id={$id} and identity_id in({$userid}))";
//        var_dump($sql);die;
        $res = $this->query($sql);
        if (empty($res)){
            return false;
        }
        //获取当前平台的身份ID
        $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from {$this->tableName} where basic_id={$id})";
        $res1 = $this->query($sql1);
        foreach ($res as $key =>$valuse ){
            foreach ($res1 as $key1 => $val1 ){
                if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                    unset($res[$key]);
                }
            }
        }
        if (!empty($res)){
            $value_str = '';
            foreach($res as $v){
				$eventid = (int)$userids['eventid'][$v['identity_id']];
                $value_str .= "('".$v['identity_id']."','".$v['gdl_userid']."','".$v['plaform_id']."','".$v['username']."','".$v['basic_id']."','".$v['usertruename']."','".$v['company_id']."','".$v['company']."','".$v['idcard']."','".$v['departmentid']."','".$eventid."','".$userids['enect_name'][$eventid]."'),";
            }

            $valueStr = rtrim($value_str,',');
            $sql2 = "INSERT into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid,eventid,event_name) values $valueStr";
//             var_dump($sql);die;
            $this->execute($sql2);
        }
        return true;
    }
	
    /* 根据分组ID获取USER表中人所有用户到考场 */

    public function getAllUserToBasicByProjecttid($basicid, $groupid)
    {
        if (empty($basicid))
            return false;

        /* 获取项目组下的用户ID */
        $cplaform = $this->getChildAdminReg();
         if (!isset($groupid))
            return false;
        try {
//            $sql = "replace into ".$this->tableName."(identity_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) ";
            $sql = "select uc.id identity_id,u.userid gdl_userid,ep.id plaform_id,u.username,{$basicid} basic_id,u.realname usertruename,uc.company_id,uc.company_name company,u.idcard,uc.departmentid ";
            $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid  left join ex_plaform as ep on uc.admin_reg=ep.admin_reg "; //排除超级管理员
            $sql .= "where uc.admin_reg in ({$cplaform}) and uc.userid>0 and uc.status=0 ";
            if(isset($groupid)&&$groupid>0) $sql .= " and uc.groupid={$groupid} ";

            if (!empty($userid)) {
                if (is_array($userid))
                    $userid = implode(',', $userid);
                $sql .= " and uc.userid in ('{$userid}') ";
            }
            $sql .= "and uc.id not in (select identity_id from ".$this->tableName." where basic_id=" . $basicid;

            if (!empty($userid))
                $sql .= " and userid in('{$userid}')";
            $sql .= ")";
            $res = $this->query($sql);
//            var_dump($sql);die;
            if (empty($res)){
                return false;
            }
            //获取当前平台的身份ID
            $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from {$this->tableName} where basic_id={$basicid})";
            $res1 = $this->query($sql1);
            foreach ($res as $key =>$valuse ){
                foreach ($res1 as $key1 => $val1 ){
                    if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                        unset($res[$key]);
                    }
                }
            }
            if (!empty($res)){
                $value_str = '';
                foreach($res as $v){
                    $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['basic_id'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."',".$v['departmentid']."),";
                }

                $valueStr = rtrim($value_str,',');
                $sql2 = "INSERT into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) values $valueStr";
//              var_dump($sql);die;
                $this->execute($sql2);
            }
           // var_dump($sql);die;
        } catch (Exception $ex) {
            \SLogger::getLogger();
            return FALSE;
        }
        return TRUE;
    }

    /* 根据专业ID获取USER表中人所有用户到培训室 */

    public function getAllUserToBasicBySpecialtyid($basicid, $specialtyid) 
    {
       if (empty($basicid))
            return false;

        /* 获取项目组下的用户ID */
        $cplaform = $this->getChildAdminReg();
         if (!isset($specialtyid))
            return false;
        try {
//            $sql = "replace into ".$this->tableName."(identity_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) ";
            $sql = "select uc.id identity_id,u.userid gdl_userid,ep.id plaform_id,u.username,{$basicid} basic_id,u.realname usertruename,uc.company_id,uc.company_name company,u.idcard,uc.departmentid ";
            $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid   left join ex_plaform as ep on uc.admin_reg=ep.admin_reg "; //排除超级管理员
            $sql .= "where uc.admin_reg  in ({$cplaform}) and uc.userid>0 and uc.status=0 ";
            if(isset($specialtyid)&&$specialtyid>0) $sql .= " and uc.specialty_id={$specialtyid} ";

            if (!empty($userid)) {
                if (is_array($userid))
                    $userid = implode(',', $userid);
                $sql .= " and uc.userid in ('{$userid}') ";
            }
            $sql .= "and uc.id not in (select identity_id from ".$this->tableName." where basic_id=" . $basicid;

            if (!empty($userid))
                $sql .= " and userid in('{$userid}')";
            $sql .= ")";
            $res = $this->query($sql);
            if (empty($res)){
                return false;
            }
            //获取当前平台的身份ID
            $sql1 = "select ep.id plaform_id,u.userid,uc.id identity_id from  ex_user_company as uc left join ex_user as u on uc.userid=u.userid left join ex_plaform as ep on uc.admin_reg=ep.admin_reg where  uc.admin_reg ='".$_SESSION['think']['reg']."' and uc.userid>0 and uc.status=0 and uc.isdelete=0 and uc.id not in (select identity_id from {$this->tableName} where basic_id={$basicid})";
            $res1 = $this->query($sql1);
            foreach ($res as $key =>$valuse ){
                foreach ($res1 as $key1 => $val1 ){
                    if (($valuse['gdl_userid']==$val1['userid'] && $valuse['plaform_id']!=$val1['plaform_id']) || empty($valuse['gdl_userid'])){
                        unset($res[$key]);
                    }
                }
            }
            if (!empty($res)){
                $value_str = '';
                foreach($res as $v){
                    $value_str .= "(".$v['identity_id'].",".$v['gdl_userid'].",".$v['plaform_id'].",'".$v['username']."',".$v['basic_id'].",'".$v['usertruename']."',".$v['company_id'].",'".$v['company']."','".$v['idcard']."',".$v['departmentid']."),";
                }

                $valueStr = rtrim($value_str,',');
                $sql2 = "INSERT into ".$this->tableName."(identity_id,gdl_userid,plaform_id,username,basic_id,usertruename,company_id,company,idcard,departmentid) values $valueStr";
//              var_dump($sql);die;
                $this->execute($sql2);
            }
        } catch (Exception $ex) {
            \SLogger::getLogger();
            return FALSE;
        }
        return TRUE;
    }

    /**
     * @param $id
     */
    public function getUserByTrainingid($id, $name = array())
    {
        $conent = ['trainingid' => $id];
        if (!empty($name)) {
            $conent = $name;
            $conent['trainingid'] = $id;
        }
        return Db::name('training_user')->where($conent)->select();
    }

    /**
     * @param $id
     */
    public function getTrainingInfoByTrainingid($id)
    {
        return $this->where(['id' => $id, 'isdelete' => 0, 'status' => 1])->find();
    }
    public function getTrainingIdBybasicid($id)
    {
        return $this->where(['examid' => $id, 'isdelete' => 0, 'status' => 1])->field('id')->find();
    }
    public function gettrainingroom() 
    {
        $time = time();
        return $this->where("UNIX_TIMESTAMP(end_time)>{$time} and UNIX_TIMESTAMP(start_time)<{$time} and isdelete=0 and admin_reg='{$_SESSION['think']['reg']}'")->select();
    }
}
